package com.strange.mdl.module.mdl.controller.app.sutras;

import com.strange.mdl.framework.common.pojo.CommonResult;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;
import com.strange.mdl.module.mdl.controller.app.sutras.vo.AppMdlSutrasChapterRespVO;
import com.strange.mdl.module.mdl.controller.app.sutras.vo.AppMdlSutrasPageReqVO;
import com.strange.mdl.module.mdl.controller.app.sutras.vo.AppMdlSutrasRespVO;
import com.strange.mdl.module.mdl.dal.dataobject.sutras.MdlSutraChaptersDO;
import com.strange.mdl.module.mdl.dal.dataobject.sutras.MdlSutrasDO;
import com.strange.mdl.module.mdl.service.sutras.MdlSutraChaptersService;
import com.strange.mdl.module.mdl.service.sutras.MdlSutrasService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.strange.mdl.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 经书")
@RestController
@RequestMapping("/mdl/sutras")
@Validated
public class AppMdlSutrasController {

    @Resource
    private MdlSutrasService sutrasService;

    @Resource
    private MdlSutraChaptersService sutraChaptersService;

    @GetMapping("/get")
    @Operation(summary = "获得经书")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppMdlSutrasRespVO> getSutras(@RequestParam("id") Long id) {
        MdlSutrasDO sutras = sutrasService.getSutras(id);
        return success(BeanUtils.toBean(sutras, AppMdlSutrasRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得经书分页")
    @PermitAll
    public CommonResult<PageResult<AppMdlSutrasRespVO>> getSutrasPage(@Valid AppMdlSutrasPageReqVO pageReqVO) {
        PageResult<MdlSutrasDO> pageResult = sutrasService.getSutrasPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppMdlSutrasRespVO.class));
    }

    // 获取章节列表
    @GetMapping("/chapter/list")
    @Operation(summary = "获取章节列表")
    @PermitAll
    public CommonResult<PageResult<AppMdlSutrasChapterRespVO>> getSutrasChapterList(@RequestParam("sutraId") Long sutraId) {
        List<MdlSutraChaptersDO> list = sutraChaptersService.getSutrasChapterList(sutraId);
        PageResult<MdlSutraChaptersDO> sutraChaptersDOPageResult = new PageResult<>(list, (long) list.size());
        return success(BeanUtils.toBean(sutraChaptersDOPageResult, AppMdlSutrasChapterRespVO.class));
    }

    // 获取章节内容
    @GetMapping("/chapter/get")
    @Operation(summary = "获取章节内容")
    @PermitAll
    public CommonResult<AppMdlSutrasChapterRespVO> getSutrasChapterContent(@RequestParam("sutraId") Long sutraId, @RequestParam("chapterId") Long chapterId) {
        MdlSutraChaptersDO sutraChaptersDO = sutraChaptersService.getSutrasChapterContent(sutraId, chapterId);
        return success(BeanUtils.toBean(sutraChaptersDO, AppMdlSutrasChapterRespVO.class));
    }
}
